import os
import tempfile
import pytest
from app import create_app, db

@pytest.fixture
def app():
    """创建并配置一个Flask应用实例用于测试"""
    # 创建临时数据库文件
    db_fd, db_path = tempfile.mkstemp()
    
    # 创建测试应用
    app = create_app({
        'TESTING': True,
        'SQLALCHEMY_DATABASE_URI': f'sqlite:///{db_path}',
        'WTF_CSRF_ENABLED': False,
    })
    
    # 创建数据库表结构
    with app.app_context():
        db.create_all()
    
    yield app
    
    # 清理测试数据库
    os.close(db_fd)
    os.unlink(db_path)

@pytest.fixture
def client(app):
    """创建测试客户端"""
    return app.test_client()

@pytest.fixture
def runner(app):
    """创建CLI测试运行器"""
    return app.test_cli_runner()

@pytest.fixture
def app_context(app):
    """创建应用上下文"""
    with app.app_context() as ctx:
        yield ctx